﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;

namespace YArchitech.Plumbing
{
	public class DataHelper
	{
		public DataHelper(TreeNode selNode, InBuildSystem buildSystem)
		{
			this.selNode = selNode;
			this.buildSystem = buildSystem;
		}

		public object GetSelectClassList()
		{
			if (this.selNode == null)
			{
				return null;
			}
			IList<ShareRPipeSystem> srpSystems = this.buildSystem.SrpSystems;
			new List<FloorSystem>();
			new List<IndoorSystem>();
			new List<CirclePipe>();
			object result;
			switch (this.selNode.Level)
			{
			case 1:
				result = srpSystems;
				break;
			case 2:
				result = srpSystems[this.selNode.Parent.Index - 2].FloorSystems;
				break;
			case 3:
				result = srpSystems[this.selNode.Parent.Parent.Index - 2].FloorSystems[this.selNode.Parent.Index - 2].IndoorSystems;
				break;
			case 4:
				result = srpSystems[this.selNode.Parent.Parent.Parent.Index - 2].FloorSystems[this.selNode.Parent.Parent.Index - 2].IndoorSystems[this.selNode.Parent.Index].CirclePipes;
				break;
			default:
				result = null;
				break;
			}
			return result;
		}

		public object GetSelectClass()
		{
			object result;
			switch (this.selNode.Level)
			{
			case 1:
			{
				int index = this.selNode.Index;
				if (index != 0)
				{
					if (index != 1)
					{
						ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[this.selNode.Index - 2];
						result = shareRPipeSystem;
					}
					else
					{
						result = this.buildSystem.ReturnTmPipe;
					}
				}
				else
				{
					result = this.buildSystem.SupplyTmPipe;
				}
				break;
			}
			case 2:
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[this.selNode.Parent.Index - 2];
				int index = this.selNode.Index;
				if (index != 0)
				{
					if (index != 1)
					{
						result = ((IList<FloorSystem>)this.GetSelectClassList())[this.selNode.Index - 2];
					}
					else
					{
						result = shareRPipeSystem.ReturnPipes;
					}
				}
				else
				{
					result = shareRPipeSystem.SupplyPipes;
				}
				break;
			}
			case 3:
				result = ((IList<IndoorSystem>)this.GetSelectClassList())[this.selNode.Index];
				break;
			case 4:
			{
				CirclePipe circlePipe = null;
				if (((IList<CirclePipe>)this.GetSelectClassList()).Count > 1)
				{
					IndoorSystem circlePipesParent = this.GetCirclePipesParent();
					int index = this.selNode.Index;
					if (index != 0)
					{
						if (index != 1)
						{
							circlePipe = ((IList<CirclePipe>)this.GetSelectClassList())[this.selNode.Index - 2];
						}
						else
						{
							result = circlePipesParent.ReturnMPipe;
						}
					}
					else
					{
						result = circlePipesParent.SupplyMPipe;
					}
				}
				else
				{
					circlePipe = ((IList<CirclePipe>)this.GetSelectClassList())[this.selNode.Index];
				}
				result = circlePipe;
				break;
			}
			default:
				result = null;
				break;
			}
			return result;
		}

		public void AddData()
		{
			object selectClass = this.GetSelectClass();
			object selectClassList = this.GetSelectClassList();
			switch (this.selNode.Level)
			{
			case 1:
			{
				ShareRPipeSystem item = new ShareRPipeSystem(selectClass as ShareRPipeSystem);
				this.buildSystem.SrpSystems.Add(item);
				this.CreateTotalPipe();
				return;
			}
			case 2:
			{
				ICollection<FloorSystem> collection = (IList<FloorSystem>)selectClassList;
				FloorSystem item2 = new FloorSystem(selectClass as FloorSystem);
				collection.Add(item2);
				this.CreateRiserPipe();
				return;
			}
			case 3:
			{
				ICollection<IndoorSystem> collection2 = (IList<IndoorSystem>)selectClassList;
				IndoorSystem item3 = new IndoorSystem(selectClass as IndoorSystem);
				collection2.Add(item3);
				return;
			}
			case 4:
			{
				ICollection<CirclePipe> collection3 = (IList<CirclePipe>)selectClassList;
				CirclePipe item4 = new CirclePipe(selectClass as CirclePipe);
				collection3.Add(item4);
				return;
			}
			default:
				return;
			}
		}

		public void DeleteData()
		{
			object selectClass = this.GetSelectClass();
			object selectClassList = this.GetSelectClassList();
			switch (this.selNode.Level)
			{
			case 1:
				if (this.buildSystem.SrpSystems.Count <= 1)
				{
					YJKMessageBox.Information("至少要有一个共用立管");
					return;
				}
				if (YJKMessageBox.Show("确定要删除[" + this.selNode.Text + "]吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					ShareRPipeSystem item = selectClass as ShareRPipeSystem;
					this.buildSystem.SrpSystems.Remove(item);
					IList<HYPipeData> supplyTmPipe = this.buildSystem.SupplyTmPipe;
					supplyTmPipe.Remove(supplyTmPipe[0]);
					IList<HYPipeData> returnTmPipe = this.buildSystem.ReturnTmPipe;
					returnTmPipe.Remove(returnTmPipe[0]);
					return;
				}
				return;
			case 2:
			{
				IList<FloorSystem> list = (IList<FloorSystem>)selectClassList;
				if (list.Count <= 1)
				{
					YJKMessageBox.Information("至少要有一层楼层");
					return;
				}
				if (YJKMessageBox.Show("确定要删除[" + this.selNode.Text + "]吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					FloorSystem item2 = selectClass as FloorSystem;
					list.Remove(item2);
					IList<HYPipeData> supplyPipes = this.buildSystem.SrpSystems[this.selNode.Parent.Index - 2].SupplyPipes;
					supplyPipes.Remove(supplyPipes[0]);
					IList<HYPipeData> returnPipes = this.buildSystem.SrpSystems[this.selNode.Parent.Index - 2].ReturnPipes;
					returnPipes.Remove(returnPipes[0]);
					return;
				}
				return;
			}
			case 3:
			{
				IList<IndoorSystem> list2 = (IList<IndoorSystem>)selectClassList;
				if (list2.Count <= 1)
				{
					YJKMessageBox.Information("至少要有一个户内系统");
					return;
				}
				if (YJKMessageBox.Show("确定要删除[" + this.selNode.Text + "]吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					IndoorSystem item3 = selectClass as IndoorSystem;
					list2.Remove(item3);
					return;
				}
				return;
			}
			case 4:
			{
				IList<CirclePipe> list3 = (IList<CirclePipe>)selectClassList;
				if (list3.Count <= 1)
				{
					YJKMessageBox.Information("至少要有一个环路");
					return;
				}
				if (YJKMessageBox.Show("确定要删除[" + this.selNode.Text + "]吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					CirclePipe item4 = selectClass as CirclePipe;
					list3.Remove(item4);
					return;
				}
				return;
			}
			default:
				return;
			}
		}

		public void BindDataGridHeader(IList<string> listHeader, DataGridView dataGridView)
		{
			foreach (string text in listHeader)
			{
				if (text == "公称外径" || text == "公称直径")
				{
					DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();
					dataGridViewComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
					dataGridViewComboBoxColumn.HeaderText = text;
					dataGridViewComboBoxColumn.Name = text;
					dataGridViewComboBoxColumn.SortMode = DataGridViewColumnSortMode.Automatic;
					if (text == "公称直径")
					{
						foreach (int num in ConstantOfGHCalc.GJB)
						{
							if (num != 0)
							{
								dataGridViewComboBoxColumn.Items.Add(num.ToString());
							}
						}
					}
					else
					{
						foreach (double num2 in ConstantOfGHCalc.XPAPWJB)
						{
							dataGridViewComboBoxColumn.Items.Add(num2.ToString());
						}
					}
					dataGridView.Columns.Add(dataGridViewComboBoxColumn);
				}
				else
				{
					DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
					if (text != "L(m)" && text != "Q")
					{
						dataGridViewTextBoxColumn.ReadOnly = true;
					}
					dataGridViewTextBoxColumn.HeaderText = text;
					dataGridViewTextBoxColumn.Name = text;
					dataGridView.Columns.Add(dataGridViewTextBoxColumn);
				}
			}
		}

		public void BindDataGridData(DataGridView dataGridView)
		{
			object selectClass = this.GetSelectClass();
			dataGridView.Rows.Clear();
			switch (this.selNode.Level)
			{
			case 0:
				dataGridView.Rows.Add(new object[]
				{
					this.buildSystem.RiserPipeNum,
					this.buildSystem.TotalLoad,
					this.buildSystem.TotalFlow,
					this.buildSystem.TotalResistance
				});
				break;
			case 1:
			{
				int index = this.selNode.Index;
				if (index != 0)
				{
					if (index != 1)
					{
						ShareRPipeSystem shareRPipeSystem = selectClass as ShareRPipeSystem;
						if (shareRPipeSystem != null)
						{
							int count = shareRPipeSystem.FloorSystems.Count;
							dataGridView.Rows.Add(new object[]
							{
								count,
								shareRPipeSystem.RateUnbalance
							});
						}
					}
					else
					{
						for (int i = 0; i < this.buildSystem.ReturnTmPipe.Count; i++)
						{
							HYPipeData hypipeData = this.buildSystem.ReturnTmPipe[i];
							dataGridView.Rows.Add(new object[]
							{
								"总回水干管" + (i + 1),
								hypipeData.Flow,
								hypipeData.Diameter.ToString(),
								hypipeData.Length,
								hypipeData.ResCoefLocal,
								hypipeData.Speed,
								hypipeData.ResistanceFrict,
								hypipeData.DragForce,
								hypipeData.ResistanceLocal,
								hypipeData.Resistance
							});
						}
					}
				}
				else
				{
					for (int j = 0; j < this.buildSystem.SupplyTmPipe.Count; j++)
					{
						HYPipeData hypipeData2 = this.buildSystem.SupplyTmPipe[j];
						dataGridView.Rows.Add(new object[]
						{
							"总供水干管" + (j + 1),
							hypipeData2.Flow,
							hypipeData2.Diameter.ToString(),
							hypipeData2.Length,
							hypipeData2.ResCoefLocal,
							hypipeData2.Speed,
							hypipeData2.ResistanceFrict,
							hypipeData2.DragForce,
							hypipeData2.ResistanceLocal,
							hypipeData2.Resistance
						});
					}
				}
				break;
			}
			case 2:
			{
				int index = this.selNode.Index;
				if (index != 0)
				{
					if (index != 1)
					{
						FloorSystem floorSystem = selectClass as FloorSystem;
						if (floorSystem != null)
						{
							dataGridView.Rows.Add(new object[]
							{
								floorSystem.IndoorNum,
								floorSystem.RateUnbalance
							});
						}
					}
					else
					{
						for (int k = 0; k < ((IList<HYPipeData>)selectClass).Count; k++)
						{
							HYPipeData hypipeData3 = ((IList<HYPipeData>)selectClass)[k];
							dataGridView.Rows.Add(new object[]
							{
								"回水立管" + (k + 1),
								hypipeData3.Flow,
								hypipeData3.Diameter.ToString(),
								hypipeData3.Length,
								hypipeData3.ResCoefLocal,
								hypipeData3.Speed,
								hypipeData3.ResistanceFrict,
								hypipeData3.DragForce,
								hypipeData3.ResistanceLocal,
								hypipeData3.Resistance
							});
						}
					}
				}
				else
				{
					for (int l = 0; l < ((IList<HYPipeData>)selectClass).Count; l++)
					{
						HYPipeData hypipeData4 = ((IList<HYPipeData>)selectClass)[l];
						dataGridView.Rows.Add(new object[]
						{
							"供水立管" + (l + 1),
							hypipeData4.Flow,
							hypipeData4.Diameter.ToString(),
							hypipeData4.Length,
							hypipeData4.ResCoefLocal,
							hypipeData4.Speed,
							hypipeData4.ResistanceFrict,
							hypipeData4.DragForce,
							hypipeData4.ResistanceLocal,
							hypipeData4.Resistance
						});
					}
				}
				break;
			}
			case 3:
			{
				IndoorSystem indoorSystem = selectClass as IndoorSystem;
				if (indoorSystem != null)
				{
					dataGridView.Rows.Add(new object[]
					{
						indoorSystem.CircleNum,
						indoorSystem.RateUnbalance,
						indoorSystem.DPressCapital,
						indoorSystem.DPressControl
					});
				}
				break;
			}
			case 4:
				if (selectClass is HYPipeData)
				{
					HYPipeData hypipeData5 = selectClass as HYPipeData;
					int index = this.selNode.Index;
					if (index != 0)
					{
						if (index == 1)
						{
							dataGridView.Rows.Add(new object[]
							{
								"户内总回水干管",
								hypipeData5.Flow,
								hypipeData5.OutDiameter,
								hypipeData5.Length,
								hypipeData5.ResCoefLocal,
								hypipeData5.Speed,
								hypipeData5.ResistanceFrict,
								hypipeData5.DragForce,
								hypipeData5.ResistanceLocal,
								hypipeData5.Resistance
							});
						}
					}
					else
					{
						dataGridView.Rows.Add(new object[]
						{
							"户内总供水干管",
							hypipeData5.Flow,
							hypipeData5.OutDiameter,
							hypipeData5.Length,
							hypipeData5.ResCoefLocal,
							hypipeData5.Speed,
							hypipeData5.ResistanceFrict,
							hypipeData5.DragForce,
							hypipeData5.ResistanceLocal,
							hypipeData5.Resistance
						});
					}
				}
				if (selectClass is CirclePipe)
				{
					CirclePipe circlePipe = selectClass as CirclePipe;
					if (circlePipe != null)
					{
						HYPipeData pipeData = circlePipe.PipeData;
						dataGridView.Rows.Add(new object[]
						{
							"盘管",
							pipeData.Load,
							circlePipe.Flow,
							pipeData.OutDiameter,
							pipeData.Length,
							pipeData.ResCoefLocal,
							pipeData.Speed,
							pipeData.ResistanceFrict,
							pipeData.Resistance,
							circlePipe.RateUnbalance
						});
					}
				}
				break;
			}
			this.FormatDataGridView(dataGridView);
		}

		private IndoorSystem GetCirclePipesParent()
		{
			return this.buildSystem.SrpSystems[this.selNode.Parent.Parent.Parent.Index - 2].FloorSystems[this.selNode.Parent.Parent.Index - 2].IndoorSystems[this.selNode.Parent.Index];
		}

		public void CreateTotalPipe()
		{
			IList<HYPipeData> supplyTmPipe = this.buildSystem.SupplyTmPipe;
			supplyTmPipe.Add(new HYPipeData(supplyTmPipe[0]));
			IList<HYPipeData> returnTmPipe = this.buildSystem.ReturnTmPipe;
			returnTmPipe.Add(new HYPipeData(returnTmPipe[0]));
		}

		public void CreateRiserPipe()
		{
			IList<HYPipeData> supplyPipes = this.buildSystem.SrpSystems[this.selNode.Parent.Index - 2].SupplyPipes;
			supplyPipes.Add(new HYPipeData(supplyPipes[0]));
			IList<HYPipeData> returnPipes = this.buildSystem.SrpSystems[this.selNode.Parent.Index - 2].ReturnPipes;
			returnPipes.Add(new HYPipeData(returnPipes[0]));
		}

		public void SaveCellData(DataGridViewCellEventArgs e, DataGridView dataGridView)
		{
			object selectClass = this.GetSelectClass();
			if (selectClass is IList<HYPipeData>)
			{
				if (dataGridView.CurrentRow.Cells["L(m)"].Value != null)
				{
					((IList<HYPipeData>)selectClass)[e.RowIndex].Length = double.Parse(dataGridView.CurrentRow.Cells["L(m)"].Value.ToString());
				}
				((IList<HYPipeData>)selectClass)[e.RowIndex].Diameter = int.Parse(dataGridView.CurrentRow.Cells["公称直径"].Value.ToString());
				return;
			}
			if (selectClass is HYPipeData)
			{
				if (dataGridView.CurrentRow.Cells["L(m)"].Value != null)
				{
					((HYPipeData)selectClass).Length = double.Parse(dataGridView.CurrentRow.Cells["L(m)"].Value.ToString());
				}
				((HYPipeData)selectClass).OutDiameter = dataGridView.CurrentRow.Cells["公称外径"].Value.ToString();
				return;
			}
			if (selectClass is CirclePipe)
			{
				if (dataGridView.CurrentRow.Cells["Q"].Value != null)
				{
					((CirclePipe)selectClass).PipeData.Load = double.Parse(dataGridView.CurrentRow.Cells["Q"].Value.ToString());
					((CirclePipe)selectClass).Load = double.Parse(dataGridView.CurrentRow.Cells["Q"].Value.ToString());
					dataGridView.CurrentRow.Cells["G(kg/h)"].Value = ((CirclePipe)selectClass).Flow;
				}
				if (dataGridView.CurrentRow.Cells["L(m)"].Value != null)
				{
					((CirclePipe)selectClass).PipeData.Length = double.Parse(dataGridView.CurrentRow.Cells["L(m)"].Value.ToString());
				}
				((CirclePipe)selectClass).PipeData.OutDiameter = dataGridView.CurrentRow.Cells["公称外径"].Value.ToString();
			}
		}

		private void FormatDataGridView(DataGridView dgv)
		{
			this.FormatColumn(dgv, "G(kg/h)", 0);
			this.FormatColumn(dgv, "L(m)", 2);
			this.FormatColumn(dgv, "Q", 0);
			this.FormatColumn(dgv, "V(m/s)", 3);
			this.FormatColumn(dgv, "R(Pa/m)", 2);
			this.FormatColumn(dgv, "ΔPy(Pa)", 2);
			this.FormatColumn(dgv, "ΔPj(Pa)", 2);
			this.FormatColumn(dgv, "ΔP(Pa)", 2);
			this.FormatColumn(dgv, "不平衡率(%)", 2);
			this.FormatColumn(dgv, "总压降(Pa)", 2);
			this.FormatColumn(dgv, "控制阀资用压力(Pa)", 2);
			this.FormatColumn(dgv, "控制阀控制压差(Pa)", 2);
			this.FormatColumn(dgv, "总流量(kg/h)", 2);
			this.FormatColumn(dgv, "总负荷(W)", 2);
		}

		private void FormatColumn(DataGridView dgv, string strColumnName, int num)
		{
			DataGridViewColumn dataGridViewColumn = dgv.Columns[strColumnName];
			if (dataGridViewColumn != null)
			{
				dataGridViewColumn.DefaultCellStyle.Format = "N" + num;
			}
		}

		public void ExportExcel(OptionData option)
		{
			Microsoft.Office.Interop.Excel.Application application = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
			Workbook workbook = application.Workbooks.Add(Missing.Value);
            //if (DataHelper.<>o__15.<>p__0 == null)
            //{
            //    DataHelper.<>o__15.<>p__0 = CallSite<Func<CallSite, object, Worksheet>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Worksheet), typeof(DataHelper)));
            //}
            //Worksheet worksheet = DataHelper.<>o__15.<>p__0.Target(DataHelper.<>o__15.<>p__0, workbook.ActiveSheet);
            //application.Visible = true;
            //Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A1", "L1");
            //range.Merge(Type.Missing);
            //range.set_Value(Type.Missing, "分户热计量水力计算表");
            //range.Font.Size = 11;
            //range.Font.Name = "黑体";
            //range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
            //int num = 3;
            //Range range2 = worksheet.get_Range("D" + num, "F" + num);
            //range2.Merge(Type.Missing);
            //range2.set_Value(Type.Missing, "设计软件:");
            //range2.Font.Size = 10;
            //range2.Font.Name = "宋体";
            //range2.Borders.LineStyle = 1;
            //range2.HorizontalAlignment = XlHAlign.xlHAlignRight;
		}

		private TreeNode selNode;

		private InBuildSystem buildSystem;
	}
}
